package everyday;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import entity.Order;
import org.junit.jupiter.api.Test;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

/**
 * 时间相关操作
 *
 * @author gaojie
 * @date 2022-04-09
 */
public class TimeOperation {

    /**
     * 计算两个日期相差的天数
     */
    @Test
    void test() {
        LocalDate start = LocalDate.of(2024, 8, 7);
        LocalDate end = LocalDate.of(2025, 4, 30);
        Period between = Period.between(start, end);
        System.out.println(between.getYears());
        System.out.println(between.getMonths());
        System.out.println(between.getDays());
        System.out.println(end.toEpochDay() - start.toEpochDay());

        final long between1 = ChronoUnit.DAYS.between(start, end);
        System.out.println(between1);
        final long until = start.until(end, ChronoUnit.DAYS);
        System.out.println(until);
    }

    /**
     * 看 LocalTime.toString() 是什么格式
     */
    @Test
    void test02() {
        final LocalTime now = LocalTime.now();
        System.out.println(now.toString().substring(0, 8));
    }

    /**
     * LocalTime 如果时间为 23:00:00，toString会默认显示为 23:00，需要 format 才能保证格式不丢失
     */
    @Test
    void test021() {
        final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        final LocalTime parse = LocalTime.parse("23:00:00");
        System.out.println(parse);
        System.out.println(parse.toString());
        System.out.println(parse.format(formatter));
    }

    /**
     * 对象包含这个格式的 string，转换成对象
     * 2023-06-09T10:31:46.000Z 字符串转换成 LocalDateTime
     */
    @Test
    void test03() {
        String text = "{\"id\": 123, \"desc\": \"我是一个订单\", \"createTime\": \"2023-06-09T10:31:46.000Z\"}";
        Order order = JSON.parseObject(text, Order.class);
        System.out.println(order);
    }

    /**
     * 2023-06-09T10:31:46.000Z 字符串转换成 LocalDateTime
     */
    @Test
    void test04() {
        String text = "2023-06-09T10:31:46.000Z";
        final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        final LocalDateTime parse = LocalDateTime.parse(text, formatter);
        System.out.println(parse);
        System.out.println(parse.getHour());
    }

    /**
     * 测试 LocalDate.toString() 是什么格式
     */
    @Test
    void test05() {
        System.out.println(LocalDate.now());
    }

    /**
     * 计算两个时间相差
     */
    @Test
    void test06() {
        final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        final LocalDateTime start = LocalDateTime.parse("2023-08-07 00:00:00", formatter);
        final LocalDateTime end = LocalDateTime.parse("2023-08-08 00:00:00", formatter);
        final long min = start.until(end, ChronoUnit.MINUTES);
        final long days = start.until(end, ChronoUnit.DAYS);
        System.out.println(min);
        System.out.println(days);
    }

    /**
     * 获取一天的最小时间，最大时间
     */
    @Test
    void test07() {
        final LocalDate endDate = LocalDate.now();
        final LocalDate startDate = endDate.plusDays(-1);
        final LocalDateTime min = LocalDateTime.of(startDate, LocalTime.MIN);
        final LocalDateTime max = LocalDateTime.of(endDate, LocalTime.MAX);
        System.out.println(min);
        System.out.println(max);
        System.out.println(LocalDateTime.MAX);
    }

    /**
     * 日期增加
     */
    @Test
    void test08() {
        final LocalDate now = LocalDate.now();
        System.out.println(now);
        final LocalDate afterDate = now.plusDays(4);
        System.out.println(afterDate);
    }

    /**
     * String 转 LocalDate
     */
    @Test
    void test09() {
        String str = "{\"loanNo\":\"AAL2023110600000032\", \"date\":\"2024-09-05\"}";
        final JSONObject jsonObject = JSON.parseObject(str);
        final LocalDate parse = LocalDate.parse("2023-11-06");
        final LocalDate parse2 = LocalDate.parse((CharSequence) jsonObject.get("date"));
        System.out.println(parse);
        System.out.println(parse2);
    }

    /**
     * 测试时间戳的长度
     * 测试结果：时间戳长度13位
     */
    @Test
    void test10() {
        System.out.println(System.currentTimeMillis());
    }

    /**
     * 判断LocalDateTime是否为当天
     * 测试结果：可以先转LocalDate然后再比较
     */
    @Test
    void test11() {
        LocalDateTime localDateTime = LocalDateTime.of(2024, 12, 9, 0, 0);
        System.out.println(localDateTime.toLocalDate());
        System.out.println(localDateTime.toLocalDate().equals(LocalDate.now()));
    }
}